package com.javaweb.dao.impl;

import com.javaweb.dao.CommentDao;
import com.javaweb.model.Comment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import static java.sql.Types.NULL;

@Repository
@Slf4j
public class CommentDaoImpl implements CommentDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int insert(Comment newComment){
        // 参数校验，确保article_id不为空
        if (newComment == null || newComment.getArticleId() == null) {
            return 0;
        }
        String sql = "INSERT INTO tb_Comment (id, content, article_id, user_id, support, oppose, create_time, update_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        Object [] args = new Object[]{
                newComment.getId(),
                newComment.getContent(),
                newComment.getArticleId(),
                newComment.getUserId(),
                newComment.getSupport(),
                newComment.getOppose(),
                new java.sql.Date(newComment.getCreateTime().getTime()),
                new java.sql.Date(newComment.getUpdateTime().getTime())}                               ;
        try {
            // 执行插入操作
            int rowsAffected = jdbcTemplate.update(sql, args);

            // 返回插入影响的行数，如果为0表示插入失败，返回1表示插入成功
            return rowsAffected > 0 ? 1 : 0;
        } catch (DataAccessException e) {
            // 记录异常信息
            log.error("Error inserting comment", e);
            return 0; // 发生异常时返回0
        }
    }

    @Override
    public int insertSupport(Integer commentId){
        String sql = "UPDATE tb_comment SET support = support + 1 WHERE  id=?";
        return jdbcTemplate.update(sql,commentId);
    }

    @Override
    public int insertOppose(Integer commentId){
        String sql = "UPDATE tb_comment SET oppose = oppose + 1 WHERE  id=?";
        return jdbcTemplate.update(sql,commentId);
    }

    @Override
    public List selectAllCommentsById(Integer articleId){
        if(articleId == NULL ){
            return null;
        }
        String sql = "SELECT * FROM tb_comment WHERE article_id=?";

        return jdbcTemplate.query(sql,new Object[]{articleId}, new RowMapper<Comment>(){
            @Override
            public Comment mapRow(ResultSet rs, int num) throws SQLException {
                Comment comment = new Comment();
                comment.setId(rs.getInt("id"));
                comment.setContent(rs.getString("content"));
                comment.setArticleId(rs.getInt("article_id"));
                comment.setUserId(rs.getInt("user_id"));
                comment.setSupport(rs.getInt("support"));
                comment.setOppose(rs.getInt("oppose"));
                comment.setCreateTime(rs.getTime("create_time"));
                comment.setUpdateTime(rs.getTime("update_time"));
                return comment;
            }
        });
    }
}
